package com.shu.driftbook.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ajaxservlet extends HttpServlet {
	
	protected void processRequest(HttpServletRequest request,HttpServletResponse response)
			throws IOException, ClassNotFoundException, SQLException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");  
		PrintWriter out = response.getWriter();  
	        try {  
	            response.setContentType("text/html");  
	            response.setHeader("Cache-Control", "no-store");  
	            response.setHeader("Pragma", "no-cache");  
	            response.setDateHeader("Expires", 0);
	            Class.forName("com.mysql.jdbc.Driver");
	            Connection con=DriverManager.getConnection("jdbc:mysql://211.101.12.143/drift?useUnicode=true&amp;characterEncoding=UTF-8","root","shuos");
	            Statement stm=con.createStatement();
	            String name = request.getParameter("q");
	            if(name!=null) {  
	            	ResultSet rs=stm.executeQuery("select * from user where username="+"'"+name+"'");
	            	while(rs.next()){
	            		out.write("yes");
	            	}
	            }  
	        }
	        finally{
	        	out.close();
	        }
        }
	@Override  
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  
        try {
			processRequest(request, response);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
    }   
  
    @Override  
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  
        try {
			processRequest(request, response);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
    }  
  
    @Override  
    public String getServletInfo() {  
        return "Short description";  
    }  
}
